package com.cn.intelligence.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.intelligence.mapper.TbItemCatMapper;
import com.cn.intelligence.pojo.TaotaoResult;
import com.cn.intelligence.pojo.TbItemCat;
import com.cn.intelligence.pojo.TbItemCatExample;
import com.cn.intelligence.service.ItemCatService;
import com.cn.intelligence.util.IDUtils;

/**
 * Created by Administrator on 2017/4/18.
 */
@Service
public class ItemCatServiceImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;
    @Override
    public List<Map<String,Object>> getItemCatList(long parentId) {
        // 查询类目标
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        TbItemCatExample.Criteria criteria=tbItemCatExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        tbItemCatExample.setOrderByClause("sort_order");
        List<TbItemCat>  tbItemCatList= tbItemCatMapper.selectByExample(tbItemCatExample);
        // 将类目标转换为树节点
//        List<EUTreeNode> euTreeNodeList = new ArrayList <>();
//        for (TbItemCat tbItemCat:tbItemCatList) {
//            EUTreeNode node = new EUTreeNode();
//            node.setId(tbItemCat.getId());
//            node.setText(tbItemCat.getName());
//            node.setState(tbItemCat.getIsParent()?"closed":"open");
//            euTreeNodeList.add(node);
//        }
        List<Map<String,Object>> treenodelist = new ArrayList<Map<String,Object>>();
        for (TbItemCat tbItemCat:tbItemCatList) {
        	Map<String,Object> node = new HashMap<String,Object>();
        	node.put("id", tbItemCat.getId());
        	node.put("text", tbItemCat.getName());
        	node.put("state",tbItemCat.getIsParent()?"closed":"open");
        	node.put("status", tbItemCat.getStatus()==1?"正常":"删除");
        	node.put("sortOrde", tbItemCat.getSortOrder());
        	node.put("isParent", tbItemCat.getIsParent()?"是":"否");
        	node.put("photoX", tbItemCat.getPhotoX());
        	treenodelist.add(node);
      }
        return treenodelist;
    }
	@Override
	public TaotaoResult addItemCat(TbItemCat tbItemCat) {
		Long id = IDUtils.genItemId();
		tbItemCat.setId(id);
		tbItemCat.setIsParent(tbItemCat.getParentId()==null?true:false);
		tbItemCat.setParentId(tbItemCat.getParentId()==null?0l:tbItemCat.getParentId());
		tbItemCat.setStatus(1);
		tbItemCat.setCreated(new Date());
		tbItemCat.setUpdated(new Date());
		int insertvalue = tbItemCatMapper.insert(tbItemCat);
		if(insertvalue == 1){
			return TaotaoResult.ok(tbItemCat);
		}else{
			return TaotaoResult.fail();
		}
	}
	@Override
	public TaotaoResult updateItemCat(TbItemCat tbItemCat) {
        TbItemCatExample tbItemCatExample = new TbItemCatExample();
        TbItemCatExample.Criteria criteria=tbItemCatExample.createCriteria();
        criteria.andIdEqualTo(tbItemCat.getId());
		tbItemCat.setUpdated(new Date());
		int update = tbItemCatMapper.updateByExampleSelective(tbItemCat, tbItemCatExample);
		if(update == 1){
			return TaotaoResult.ok(tbItemCat);
		}else{
			return TaotaoResult.fail();
		}
	}
	@Override
	public TaotaoResult deleteItemCat(Long id) {
		int flag = tbItemCatMapper.deleteByPrimaryKey(id);
		if(flag == 1){
			return TaotaoResult.ok();
		}else{
			return TaotaoResult.fail();
		}        
	}
	@Override
	public TaotaoResult getItemCatDesc(String catid) {
		TbItemCat cat = tbItemCatMapper.selectByPrimaryKey(Long.parseLong(catid));
		return TaotaoResult.ok(cat);
	}
}
